package com.czl.module_service.adapter

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.czl.base.binding.command.BindingCommand
import com.czl.base.binding.command.BindingConsumer
import com.czl.base.data.bean.tengyun.AssetHandleFacilityBean
import com.czl.base.data.bean.tengyun.AssetHandleSortBean
import com.czl.module_service.R
import com.czl.module_service.databinding.ItemAssetHandleSortInuseBinding

class AssetHandleSortInuseAdapter :
    BaseQuickAdapter<AssetHandleSortBean, BaseDataBindingHolder<ItemAssetHandleSortInuseBinding>>(
        R.layout.item_asset_handle_sort_inuse
    ) {

    var productIdList: ArrayList<Int> = arrayListOf()

    override fun convert(
        holder: BaseDataBindingHolder<ItemAssetHandleSortInuseBinding>,
        item: AssetHandleSortBean,
    ) {
        holder.dataBinding?.run {
            if (item.manageMode == 1) {
                item.sortName = item.productName
                item.sortCode = item.productCode
                item.sortModel = item.productModel
                item.selectedInList = productIdList.contains(item.productId)
                item.sortNameTemp = item.sortName
                if (item.selected == null) {
                    item.selected = false
                }
                item.assetCount = 1
            } else {
                item.sortNameTemp = item.sortName + "（"+ item.useNum +"）"
            }
            item.sortNo = item.sortCode
            item.model = item.sortModel
            item.houseStoreName = item.storehouseName
            item.locationDesc = item.storehouseLocatName
            item.position = holder.layoutPosition
            if (item.assetCount == null) {
                item.assetCount = 0
            }
            if (item.assetCount!! > 0) {
                item.assetCountStr = ""+item.assetCount
            }
            data = item
            adapter = this@AssetHandleSortInuseAdapter
            executePendingBindings()
        }
    }

    val onItemClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is AssetHandleSortBean) {
            if (it.manageMode == 1  && !it.selectedInList!!) {
                var item: AssetHandleSortBean = it
                item.selected = !item.selected!!
                setData(item.position!!, item)
            }
        }
    })
}
